const util = require('../../../utils/util.js');
const api = require('../../../utils/api.js');
const App = getApp();

//我的收藏页面
Page({
  data: {
    defaultimg: 'http://oss-img.meitianhui.com/test/communityDocs/201801/51a75f1fc3f445cba238b145faff1d41.jpg',
    shoplist: '',
    consumer_id:'',
    goodsType:'',
    buttonType:'',
    params: {
      page: 1,
      limit: 8
    },
    modalHidden: true,
    rps_data:"",
    goods_id:''

  },

  //初始化数据加载
  onLoad: function (option){
    this.setData({
      consumer_id: option.consumer_id,
      goodsType: option.goodsType,
      goods_id: option.goods_id
    })
    this.getList()
  },

  getList() {
    const self = this;

    let data = {
      service: 'stores.consumer.favoriteStoreList',
      params: '{"consumer_id":""}',
      url_type: 'member',
      app_token: App.globalData.app_token,
      security_code: App.globalData.security_code 
    }

    var jsons = JSON.parse(data.params);
    jsons["consumer_id"] = self.data.consumer_id;
    data.params = JSON.stringify(jsons);

    //标签状态切换查询
    //let sign = util.getSign(data);
    //data.sign = sign;
    api.reqData({
      data,
      success: (res) => {
        //这个得写法和格式 setData({........})
        console.log(res.data);

        //处理过滤数据，根据
        var json = res.data.data.list;
        var doc_url = res.data.data.doc_url;
        var doc_url_key = Object.keys(doc_url)
        for(var i=0;i<json.length;i++){
          var lat1 = json[i].latitude;
          var lng1 = json[i].longitude;
          var lat2 = App.globalData.locationInfo.latitude;
          var lng2 = App.globalData.locationInfo.longitude;
          var distance = util.getDistance(lat1, lng1, lat2, lng2).toFixed(1);
          //一定要分清楚是js数组还是js对象添加元素
          json[i]["distance"] = distance;
          //adress过长得情况在前台处理
          let address = json[i].address;
          if (address.indexOf("/") != -1) {
            let start = address.lastIndexOf('/')+1;
            json[i].address = address.substring(start);
          }

          //订单头像图片展示  doc_url和里面头像分离
          var neighbor_pic_path_str = json[i].neighbor_pic_path;
          if (neighbor_pic_path_str != '') {
            for (var e = 0; e < doc_url_key.length; e++) {
              if (neighbor_pic_path_str == doc_url_key[e]) {
                json[i].neighbor_pic_path = doc_url[doc_url_key[e]].replace("https", "http")
              }
            }
          } else {
            json[i].pic_info = ''
          }
          
          
        }
        console.log("json="+json);
        res.data.data.list = json;
        self.setData({
          rps_data: res.data.data,
        })
      },
    })

  },


  //页面弹出半透明窗口js
  modalTap: function (e) {
    var stores_id = e.currentTarget.dataset.id;
    this.setData({
      modalHidden: false,
      stores_id: stores_id
    })
  },
  modalChange: function (e) {
    //确认按钮调用 默认门店接口
    const self = this;
    let data = {
      service: 'stores.consumer.favoriteStore',
      params: '{"consumer_id":"11432431","is_llm_stores":"Y","stores_id":"cacabbe3-da98-11e5-a165-00163e010763"}',
      url_type: 'member',
      app_token: App.globalData.app_token,
      security_code: App.globalData.security_code 
    }

    var json = JSON.parse(data.params);
    //淘淘领传值
    var stores_id = e.currentTarget.dataset.id;
    json["stores_id"] = e.currentTarget.dataset.id;
    json["consumer_id"] = App.globalData.memberInfo.member_id;
    data.params = JSON.stringify(json);

    //let sign = util.getSign(data);
    //data.sign = sign;
    api.reqData({
      data,
      success: (res) => {
        //这个得写法和格式 setData({........})
        console.log(res.data + "---------------------------");
        var goodsType = this.data.goodsType
        var goods_id = this.data.goods_id
        if (res.data.rsp_code === "succ") {
            //避免后退是还出现弹出框，先隐藏弹出框在跳转
            this.setData({
              modalHidden: true
            })

            if(goodsType==1){
              wx.navigateTo({
                url: '../detail/taotaoling/taotaoling?goods_id=' + goods_id + '&stores_id=' + stores_id
              })
            }else{
              wx.navigateTo({
                url: '../submitOrder/submitOrder?goods_id=' + goods_id
              })
            }          
        } else {
          this.setData({
            modalHidden: true
          })
        }
      },
  })
},

  modalChange1: function (e) {
    this.setData({
      modalHidden: true
    })
  },
  
})

